package com.base.cn.platform.os.controller.qa.qareply;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.qa.qareply.QaReply;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralService;
import com.base.cn.platform.os.service.qa.qareply.QaReplyBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;


/**
 * 问题评论Controller
 * @author LIZHIPENG
 * @since 2018/4/26
 */
@RestController
@RequestMapping("/course")
public class QaReplyController extends BaseController {

    @Autowired
    private QaReplyBiz qaReplyBiz;
    @Autowired
    private CusUserIntegralService cusUserIntegralService;

    /**
     * 查询问题评论数量
     * @param qaReply 条件
     * @return BigDecimal
     */
    @RequestMapping("/replyCount")
    public BigDecimal replyCount(@RequestBody QaReply qaReply){
        return qaReplyBiz.replyCount(qaReply);
    }

    /**
     * 分页查询问题评论列表
     * @param currentPage 页码
     * @param qaReply 查询条件
     * @param createUserData 是否设置评论学员信息（true：是，false：否）
     * @param createUserData 是否设置子评论（true：是，false：否）
     * @return PageInfo<QaReply>
     */
    @RequestMapping("/findQaReplyListPage")
    public PageInfo<QaReply> findQaReplyListPage(@RequestParam(value = "currentPage", required = false, defaultValue = "1")int currentPage,
                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10")int pageSize,
                                                @RequestBody QaReply qaReply,
                                                @RequestParam("createUserData")boolean createUserData,
                                                @RequestParam("childData") boolean childData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        return qaReplyBiz.findQaReplyListPage(qaReply, page, createUserData, childData);
    }

    /**
     * 问题评论
     * @param qaReply 评论数据
     * @return Map<String, Object>
     */
    @RequestMapping("/saveQaReply")
    public Map<String, Object> saveQaReply(@RequestBody QaReply qaReply){
        if(ObjectUtils.isEmpty(qaReply)){
            return ResultUtil.ERROR("系统繁忙，请稍后重试");
        }
        if(!DataUtil.idIsNotNull(qaReply.getQuestionId())){
            return ResultUtil.ERROR("请选择要评论的问题");
        }
        if(!DataUtil.idIsNotNull(qaReply.getUserId())){
            return ResultUtil.ERROR("请登录后再来评论吧");
        }
        if(StringUtils.isEmpty(qaReply.getReplyContext()) || "".equals(qaReply.getReplyContext().trim())){
            return ResultUtil.ERROR("评论内容不能为空");
        }
        if(qaReply.getReplyContext().length() > 200){
            return ResultUtil.ERROR("评论内容不能超过200个字");
        }
        qaReply.setStatus(1);
        qaReplyBiz.saveQaReply(qaReply);
        //回复添加积分
        cusUserIntegralService.addUserIntegral(ActionKey.ANSWER,qaReply.getUserId(),qaReply.getQuestionId(),0,2);
        return ResultUtil.SUCCESS("评论成功", qaReply);
    }

    /**
     * 判断用户是否回答过问答
     * @param userId 用户ID
     * @param qaId 问答 ID
     * @return true回复过，false未回复
     */
    @RequestMapping("/isUserReply")
    public boolean isUserReply(@RequestParam("userId") BigDecimal userId,@RequestParam("qaId") BigDecimal qaId){
        return qaReplyBiz.isUserReply(userId,qaId);
    }

    /**
     * 查询用户回复的问答量
     * @param userId 用户ID
     * @return 回复的问答数量
     */
    @RequestMapping("/queryUserReplyQaNum")
    public int queryUserReplyQaNum(@RequestParam("userId")BigDecimal userId){
        return qaReplyBiz.queryUserReplyQaNum(userId);
    }
}
